// Author:zhaojunjiang
#ifndef TOKENMANAGER_H
#define TOKENMANAGER_H
#include "surbasic/Type.h"
#include "surbasic/AccessToken.h"
#include "surbasic/Mutex.hpp"
using namespace SDBasic::token;
using namespace SDBasic::lock;
namespace SDBasic {
namespace tokenmg {

class TokenExpirationHandler {
 public:
  TokenExpirationHandler() {}
  virtual ~TokenExpirationHandler() {}
  virtual void HandleExpiration(const AccessToken& token) = 0;
};

class TokenManager {
 public:
  TokenManager(void);
  ~TokenManager(void);
  static TokenManager& get();
  uint64_t SetToken(const AccessToken& _token);
  bool GetToken(const uint64_t& key, AccessToken& token);
  void ForceRefreshToken(const AccessToken& token);
  void SetExpirationHandler(TokenExpirationHandler* handler) {
    handler_ = handler;
  }

 private:
  MutexLock lock_;
  static TokenManager* ins_;
  typedef std::map<uint64_t, AccessToken> TokenSet;
  TokenSet tokenset_;
  uint64_t next_key_;
  TokenExpirationHandler* handler_;
};
}
}  // namespace
#endif
